package com.ibeeking.found.upms.b.rest.controller.v1;

import com.ibeeking.found.upms.b.rest.service.IOpenUrlRelationService;
import com.ibeeking.found.upms.b.rest.service.IOpenUserService;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.upms.service.common.bo.OpenUrlRelationBO;
import com.ibeeking.found.upms.service.common.param.OpenUserParam;
import com.ibeeking.found.upms.service.common.param.OpenUserUrlParam;
import com.ibeeking.found.upms.service.common.query.OpenUserPageQuery;
import com.ibeeking.found.upms.service.common.vo.OpenUserPageVO;
import com.ibeeking.found.upms.service.common.vo.OpenUserVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName OpenUserController控制器
 * @Description open
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/openuser/v1")
public class OpenUserController {

    @Resource
    private IOpenUserService openUserServiceImpl;
    @Resource
    private IOpenUrlRelationService openUrlRelationServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询open用户列表")
    @PostMapping(value = "/pageList")
    public Page<OpenUserPageVO> pageList(@RequestBody OpenUserPageQuery query) {
        return openUserServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增外部用户")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) OpenUserParam param, @RequestHeader(HeaderNounConstant.TENANT_ID) String tenantId) {
        return openUserServiceImpl.add(param, tenantId);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改外部用户")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) OpenUserParam param) {
        param.setAppSecret(null);
        param.setReadSecret(null);
        return openUserServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询外部用户")
    @GetMapping(value = "/view/{id}")
    public OpenUserVO view(@PathVariable("id") Long id) {
        return openUserServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "启用/禁用外部用户")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return openUserServiceImpl.changeStatus(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除外部用户")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return openUserServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "显示秘钥")
    @GetMapping(value = "/showSecret/{id}", produces = "application/json;charset=UTF-8")
    public String showSecret(@PathVariable("id") Long id) {
        return openUserServiceImpl.showSecret(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "重置秘钥")
    @GetMapping(value = "/resetSecret/{id}")
    public Boolean resetSecret(@PathVariable("id") Long id, @RequestHeader(HeaderNounConstant.TENANT_ID) String tenantId) {
        return openUserServiceImpl.resetSecret(id, tenantId);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "开放用户关联openurl")
    @PostMapping(value = "/modifyByOpenUserId")
    public Boolean modifyByOpenUserId(@RequestBody @Validated OpenUserUrlParam param) {
        return openUrlRelationServiceImpl.modifyByOpenUserId(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询选中OpenUrlId列表")
    @GetMapping(value = "/queryOpenUrlIdsByUserId/{openUserId}")
    public List<String> queryOpenUrlIdsByUserId(@PathVariable("openUserId") @NotNull(message = "openUserId不能为空") Long openUserId) {
        List<OpenUrlRelationBO> openUrlRelationBos = openUrlRelationServiceImpl.queryByUserId(openUserId);
        return openUrlRelationBos.parallelStream().map(openUrlRelationBO -> String.valueOf(openUrlRelationBO.getOpenUrlId())).distinct().collect(Collectors.toList());
    }

}
